<template>
    <div class="ele-body">
      <el-card shadow="never">
        <!-- 搜索表单 -->
        <el-form
          :model="where"
          label-width="77px"
          class="ele-form-search"
          @keyup.enter.native="reload"
          @submit.native.prevent>
          <el-row :gutter="15">
            <el-col :lg="5" :md="12">
                <el-form-item label="订单编号:">
                  <el-input
                    clearable
                    v-model="where.danjNo"
                    placeholder=""/>
                </el-form-item>
              </el-col>
              <el-col :md="5" :sm="12">
              <el-form-item label="起始日期:">
                  <el-date-picker v-model="where.riqiBegin" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :md="5" :sm="12">
              <el-form-item label="结束日期:">
                  <el-date-picker v-model="where.riqiEnd" type="date" format='yyyy-MM-dd' value-format='yyyy-MM-dd' placeholder="选择日期" clearable class="ele-fluid"></el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="12">
                <el-form-item label="批号:">
                  <el-input
                    clearable
                    v-model="where.lotRequest"
                    placeholder=""/>
                </el-form-item>
              </el-col>
            <el-col :lg="5" :md="12">
                <el-form-item label="订单类型:">
                  <el-select
                  clearable
                  v-model="where.orderType"
                  placeholder="请选择订单类型"
                  @change="changeUrl()"
                  class="ele-fluid">
                  <el-option label="销售订单" value="c37infckkpdbill"/>
                  <el-option label="销售退货" value="c37infrkkpdbill"/>
                </el-select>
                </el-form-item>
  
              </el-col>
          </el-row>
          <el-row :gutter="15">
            <el-col :lg="5" :md="12">
                <el-form-item label="商品编号:">
                  <el-input
                    clearable
                    v-model="where.shangpNo"
                    placeholder=""/>
                </el-form-item>
              </el-col>
              <el-col :lg="5" :md="12">
                <el-form-item label="单位名称:">
                  <el-input
                    clearable
                    v-model="where.danwName"
                    placeholder=""/>
                </el-form-item>
              </el-col>
            <el-col :lg="5" :md="12">
                <el-form-item label="状态:">
                  <el-select
                  clearable
                  v-model="where.zt"
                  placeholder="请选择状态"
                  class="ele-fluid">
                  <el-option label="未同步" value="Y"/>
                  <el-option label="已同步" value="N"/>
                  <el-option label="E" value="E"/>
                  <el-option label="H" value="H"/>
                  <el-option label="ZT" value="ZT"/>
                </el-select>
                </el-form-item>
              </el-col>
  
            <el-col :lg="4" :md="12">
              <div class="ele-form-actions">
                <el-button
                  type="primary"
                  icon="el-icon-search"
                  class="ele-btn-icon"
                  size="small"
                  @click="reload">查询
                </el-button>
                <el-button @click="reset" size="small">重置</el-button>
              </div>
            </el-col>
          </el-row>
        </el-form>
        <!-- 数据表格 -->
        <ele-pro-table
          ref="table"
          :where="where"
          :datasource="url"
          :columns="columnsOrder"
          :selection.sync="selection"
          :toolbar="false"
          :needPage="false"
          highlight-current-row
          @selection-change = "handleSelectionChange()"
          @select="handleSelect"
          @row-click="loadDetail"
          height="calc(100vh - 433px)">
          <!-- 表头工具栏 -->
          <template slot="toolbar" v-if="false">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              class="ele-btn-icon"
              @click="openEdit(null)"
              v-if="permission.includes('sys:c37infckkpdbill:add')">添加
            </el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              class="ele-btn-icon"
              @click="removeBatch"
              v-if="permission.includes('sys:c37infckkpdbill:dall')">删除
            </el-button>
          </template>
          <!-- 操作列 -->
          <template slot="action" slot-scope="{row}">
            <el-link
              type="primary"
              :underline="false"
              icon="el-icon-edit"
              @click="openEdit(row)"
              v-if="permission.includes('sys:c37infckkpdbill:edit')">修改
            </el-link>
            <el-popconfirm
              class="ele-action"
              title="确定要删除此销售订单表吗？"
              @confirm="remove(row)">
              <el-link
                type="danger"
                slot="reference"
                :underline="false"
                icon="el-icon-delete"
                v-if="permission.includes('sys:c37infckkpdbill:delete')">删除
              </el-link>
            </el-popconfirm>
          </template>
        </ele-pro-table>
        <ele-pro-table
          ref="tableDetail"
          :datasource="urlDetail"
          :where="whereDetail"
          :columns="columns"
          :needPage = "false"
          :toolbar="false"
          height="calc(100vh - 515px)">
        </ele-pro-table>      
  
      </el-card>
      <!-- 编辑弹窗 -->
      <!-- <c37infckkpdbill-edit
        :data="current"
        :visible.sync="showEdit"
        @done="reload"/> -->
    </div>
  </template>
  
  <script>
  import { mapGetters } from "vuex";
  // import C37InfCkKpdBillEdit from './c37infckkpdbill-edit';
  
  export default {
    name: 'ExamC37InfCkKpdBill',
    // components: {C37InfCkKpdBillEdit},
    computed: {
      ...mapGetters(["permission"]),
    },
    data() {
      return {
        // 表格数据接口
        url: '/c37infckkpdbill/index',
        urlDetail: '/c37infckkpdbill/detail',
        // 表格列配置
        columnsOrder: [
          // {
          //   columnKey: 'selection',
          //   type: 'selection',
          //   width: 45,
          //   align: 'center',
          //   fixed: "left"
          // },
          {
            columnKey: 'index',
            type: 'index',
            width: 45,
            align: 'center',
            showOverflowTooltip: true,
          },
          {
            prop: 'danjNo',
            label: '订单编号',
            showOverflowTooltip: true,
            minWidth: 140,
            align: 'center'
          },
          {
            prop: 'kaipTime',
            label: '订单日期',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'danwNo',
            label: '客户编码',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'danwName',
            label: '客户名称',
            showOverflowTooltip: true,
            minWidth: 220,
            align: 'center'
          },
          {
            prop: 'yewStaff',
            label: '业务员',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'zt',
            label: '状态',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center',
            formatter: (row, column, cellValue) => {
              return this.statusFormat(row, column,cellValue);
            }
          },
          // {
          //   prop: 'erjiDanwNo',
          //   label: '二级单位编号',
          //   showOverflowTooltip: true,
          //   minWidth: 150,
          //   align: 'center'
          // },
          // {
          //   prop: 'kehNotes',
          //   label: '客户备注',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'hanghao',
          //   label: '行号',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'shangpNo',
          //   label: '商品编号',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'num',
          //   label: '数量',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'price',
          //   label: '价格',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'lotRequest',
          //   label: '批号要求',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'tihWay',
          //   label: '提货方式',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          // {
          //   prop: 'fapNo',
          //   label: '金税票号',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
          {
            prop: 'khZzbm',
            label: 'NC组织编码',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'xierTime',
            label: '写入时间',
            showOverflowTooltip: true,
            minWidth: 140,
            align: 'center'
          },
          // {
          //   prop: 'dizId',
          //   label: 'DIZ编码',
          //   showOverflowTooltip: true,
          //   minWidth: 100,
          //   align: 'center'
          // },
        ],
        columns: [
          // {
          //   columnKey: 'selection',
          //   type: 'selection',
          //   width: 45,
          //   align: 'center',
          //   fixed: "left"
          // },
          {
            prop: 'hanghao',
            label: '行号',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'shangpNo',
            label: '物料编码',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'chineseName',
            label: '物料名称',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'num',
            label: '数量',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'price',
            label: '价格',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'lotRequest',
            label: '批号要求',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'zt',
            label: '同步状态',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'tihWay',
            label: '提货方式',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
          {
            prop: 'fapNo',
            label: '金税票号',
            showOverflowTooltip: true,
            minWidth: 100,
            align: 'center'
          },
        ],
        // 表格搜索条件
        where: {riqiBegin:this.getFromDate(),riqiEnd:new Date(),orderType:'c37infckkpdbill'},
        whereDetail:{danjNo:'*000*'},
        // 表格选中数据
        selection: [],
        orderDetail:[],
        // 当前编辑数据
        current: null,
        // 是否显示编辑弹窗
        showEdit: false,
      };
    },
    methods: {
      statusFormat(row, column,cellValue) {
          const daterc = row[column.property]
          if(daterc !=null){
              return cellValue=="N"?"已同步":"未同步";
              }
      },
      getFromDate() {
        const timeOne = new Date()
        const year = timeOne.getFullYear()
        let month = timeOne.getMonth()
        let day = 1
        //month = month < 10 ? '0' + month : month
        //day = day < 10 ? '0' + day : day
        ///const NOW_MONTHS_AGO = `${year}-${month}-${day}`
        return new Date(year,month,day);
      },
      changeUrl()
      {
        this.url = '/'+this.where.orderType+'/index';
        this.urlDetail = '/'+this.where.orderType+'/detail';
        let param = {danjNo:'*000*'};
        this.$refs.tableDetail.reload({where: param});
        this.orderDetail=[];
      },
      handleSelectionChange()
      {
        // console.log(this.selection);
        // let currentRow = this.selection[0];
        // this.$refs.table.toggleRowSelection(currentRow,true);
        // this.loadDetail(currentRow);
      },
      handleSelect(selection){
      if(selection.length > 1){
          let del_row = selection.shift()
          this.$refs.table.toggleRowSelection(del_row,false) //设置这一行取消选中
        }
      },
      loadDetail(row){
        //let row1 = this.$refs.table.currentRow;
        //let currentSelect = this.$refs.table.selection;
        //this.$refs.table.clearSelection(); //清除所有选中数据
        //this.$refs.table.toggleRowSelection(row); //设置选中行高亮
        //console.log(row);
        this.whereDetail= {danjNo:row.danjNo};
        this.$refs.tableDetail.reload({where: this.whereDetail});
        // this.$http.get('/'+this.where.orderType+'/detail?danjNo='+row.danjNo).then(res => {
        //   if (res.data.code === 0) {
        //     //this.reload();
        //     this.orderDetail = res.data.data.records;
        //   } else {
        //     this.$message.error(res.data.msg);
        //   }
        // }).catch(e => {
        //   this.$message.error(e.message);
        // });
  
      },    
      /* 刷新表格 */
      reload() {
        this.whereDetail={danjNo:'*000*'};
        this.$refs.table.reload({where: this.where});
        this.$refs.tableDetail.reload({where: this.whereDetail});
      },
      /* 重置搜索 */
      reset() {
        this.where = {riqiBegin:this.getFromDate(),riqiEnd:new Date(),orderType:'c37infckkpdbill'};
        this.reload();
      },
      /* 显示编辑 */
      openEdit(row) {
        this.current = row;
        this.showEdit = true;
      },
      /* 删除 */
      remove(row) {
        const loading = this.$loading({lock: true});
        this.$http.delete('/c37infckkpdbill/delete', {id: row.id}).then(res => {
          loading.close();
          if (res.data.code === 0) {
            this.$message.success(res.data.msg);
            this.reload();
          } else {
            this.$message.error(res.data.msg);
          }
        }).catch(e => {
          loading.close();
          this.$message.error(e.message);
        });
      },
      /* 批量删除 */
      removeBatch() {
        if (!this.selection.length) {
          this.$message.error('请至少选择一条数据');
          return;
        }
        this.$confirm('确定要删除选中的销售订单表吗?', '提示', {
          type: 'warning'
        }).then(() => {
          const loading = this.$loading({lock: true});
          this.$http.delete('/c37infckkpdbill/delete', {id: this.selection.map(d => d.id)}).then(res => {
            loading.close();
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              this.reload();
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            loading.close();
            this.$message.error(e.message);
          });
        }).catch(() => {
        });
      },
    }
  }
  </script>
  
  <style scoped>
  </style>
  